// pages/invited/invited.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
const http = require('../../utils/util.js')
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        schoolId: ''
    },
    //課程詳情
    gocourseDetail: function(e) {
        console.log(e, '5201314')
        let course = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/classdetail/index?courseId=' + course,

        });

    },
    //  单个机构统计信息
    singleInfo: function(options) {
        let that = this
        var schoolTotal = loginFn()
        wx.request({
            url: urls.schoolTotal,
            method: 'GET',
            data: { schoolId: options.schoolId },
            success: function(res) {
                if (!app.globalData.allselect.courseMain) {
                    var int = setInterval(function() {
                        console.log('no-data')
                        if (app.globalData.allselect.courseMain) {
                            clearInterval(int);
                            that.detailFn(res.data.data);
                        }
                    }, 100);
                } else {
                    that.detailFn(res.data.data)
                }
            }
        })
    },
    detailFn: function(details) {
        var that = this;
        var sels = app.globalData.allselect;
        for (var key in details) {
            if (key == 'schoolScaleId' && sels.schoolScale) { //学校规模
                for (var j = 0; j < sels.schoolScale.length; j++) {
                    if (details[key] == sels.schoolScale[j].schoolScaleId) {
                        details[key] = sels.schoolScale[j].schoolScale
                    };
                };
            } else if (key == 'major' && sels.subject) { //专业方向
                for (var j = 0; j < sels.subject.length; j++) {
                    if (details[key] == sels.subject[j].subjectId) {
                        details[key] = sels.subject[j].subject
                    };
                };
            } else if (key == 'schoolSortId' && sels.schoolSort) { //办学形式
                for (var j = 0; j < sels.schoolSort.length; j++) {
                    if (details[key] == sels.schoolSort[j].schoolSortId) {
                        details[key] = sels.schoolSort[j].sortName
                    };
                };
            } else if (key == 'score') { //分数
                details[key] = parseFloat(details[key])
            } else if (key == 'schoolTypeId' && sels.schoolType) { //专业方向
                for (var j = 0; j < sels.schoolType.length; j++) {
                    if (details[key] == sels.schoolType[j].schoolTypeId) {
                        for (var h = 0; h < sels.schoolType[j].subType.length; h++) {
                            if (sels.schoolType[j].subType[h].typeId2 == details['typeId2']) {
                                details['typeId2'] = sels.schoolType[j].subType[h].typeName
                                break
                            };
                        }
                        break
                    };
                };
            } else if (key == 'focus') {
                if (details[key]) {
                    this.setData({
                        haslike: true
                    })
                } else {
                    this.setData({
                        haslike: false
                    })
                };
            }
        }

        var imgUrls = []
        imgUrls.push(details.logoUrl)

        // for (var i = 0; i < details.schoolImages.length; i++) {
        // 	imgUrls.push(details.schoolImages[i].img)
        // }

        that.setData({
            listword: details
        })
       
    },

    /**
     * 生命周期函数--监听页面加载
     */
    loadOptions: {},
    onLoad: function(options) {
        console.log('options参数', options)
        this.setData({
            schoolId: options.schoolId
        });
        this.loadOptions = options;
        this.singleInfo(options);
        this.checkLogin();
    },
    checkLogin(option){
    	wx.showLoading();
		var login = setInterval(() => {
			console.log('checklogin')
			if(wx.getStorageSync('login')){
				clearInterval(login);
				this.myList(option);
			}
		}, 100);
	},
    myList: function(options) {
		let that = this

        app.request({
            url: 'teacher/checkJoinStatus',
            data: {
                school_id: that.data.schoolId
            },
            sCallback: (res) => {
                wx.hideLoading();
                // if(res.code != 1) {
                //     app.alert(res.msg, () => {
                //         wx.redirectTo({
                //             url: '/pages/teacher-info/index'
                //         })
                //     })
                // }
            }
        })
	},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    //确定加入
    join() {
        var that = this;
        wx.showModal({
            title: '提示',
            content: '确定加入该机构吗?',
            success: function(e) {
                if (e.confirm) {
                    var postData = loginFn();
                    postData.schoolId = that.data.schoolId;
                    postData.status = 0;
                    wx.request({
                        url: urls.saveDel,
                        method: 'GET',
                        data: postData,
                        success: function(res) {
                            console.log(res, '加入机构')
                            if (res.data.code == '0') {
                                wx.showModal({
                                    title: '提示',
                                    content: '提交成功',
                                    showCancel: false,
                                    success: function() {
                                        wx.redirectTo({
                                            url: '/pages/teacher-info/index'
                                        });
                                    }
                                });
                            }
                        }
                    })
                }
            }
        })
    }
})